/*
 * Created by JFormDesigner on Tue May 18 10:23:20 CST 2021
 */

package com.dfrz.bookstore.view;

import com.dfrz.bookstore.entry.Admin;
import com.dfrz.bookstore.entry.Book;
import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.UserService;
import com.dfrz.bookstore.service.impl.AdminServiceImpl;
import com.dfrz.bookstore.service.impl.UserServiceImpl;
import com.dfrz.bookstore.util.CommenUtil;
import com.dfrz.bookstore.util.RegularUtiles;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Brainrain
 */
public class Login extends JFrame {
    UserServiceImpl userService = new UserServiceImpl();

    public static void main(String[] args) {
        Login login =new Login();
        login.setVisible(true);
        login.setTitle("KY在线书城 登录界面");
    }
    public Login() {
        initComponents();
    }

    /**
     * 清空 用户名 密码 输入框
     * @param e
     */
    private void button2ActionPerformed(ActionEvent e) {
        username.setText("");
        password.setText("");
        username2.setText("");
        password2.setText("");
    }

    /**
     * 用户通过账户密码登录事件
     * @param e
     */
    private void UserLoginByPassWordActionPerformed(ActionEvent e) {

        String usernameVal = username.getText(); //获取用户名
        String passwordVal = new String(password.getPassword()); //获取密码

        if (CommenUtil.inNull(usernameVal)){
            JOptionPane.showMessageDialog(null,"用户名为空");
            return; //不往下执行
        }
        if (CommenUtil.inNull(passwordVal)){
            JOptionPane.showMessageDialog(null,"密码为空");
            return; //不往下执行
        }

        //通过 用户名和密码 + Jdbc 判断是否有该用户
        User user = new User();
        user.setLoginName(usernameVal);
        user.setPassWord(passwordVal);
        User userLogin = userService.loginBypassWord(user);
        if (userLogin!=null){
            /*只给用户展示上架的图书*/
            Book book = new Book();
            book.setFlag(1);
            Client client = new Client(userLogin,book);
            client.setVisible(true);
            //关闭登录界面
            setVisible(false);
        }
    }

    /**
     *   管理员登录按钮事件
     * @param e
     */
    private void AdminLoginButtonActionPerformed(ActionEvent e) {
        AdminServiceImpl adminService = new AdminServiceImpl();
        Admin admin = new Admin();
        if (CommenUtil.inNull(username2.getText())){
            JOptionPane.showMessageDialog(null,"用户名为空");
            return; //不往下执行
        }
        if (CommenUtil.inNull(new String(password2.getPassword()))){
            JOptionPane.showMessageDialog(null,"密码为空");
            return; //不往下执行
        }
        admin.setUsername(username2.getText());
        admin.setPassword(new String(password2.getPassword()));
        Admin adminLogin = adminService.loginBypassWord(admin);
        if (adminLogin!=null){
            AdminJFrame adminJFrame = new AdminJFrame(adminLogin,new Book());
            adminJFrame.setVisible(true);
            setVisible(false);
        }
    }

    /**
     * 点击关闭 关闭登录页面
     * @param e
     */
    private void thisWindowClosed(WindowEvent e) {
        dispose();
    }

    /**
     * 发送验证码按钮
     * @param e
     */
    String varCode = null;//验证码定义全局变量
    private void sendVarCodeActionPerformed(ActionEvent e) {

        String emailStr = emailText.getText();
        /*校验邮箱*/
        if (!RegularUtiles.regEmail(emailStr)){
            return;
        }
        varCode = userService.sendVarCode(emailStr);
    }

    /**
     * 邮箱登录按钮
     * @param e
     */
    private void LoginByEamliButtonActionPerformed(ActionEvent e) {

        String emailStr = emailText.getText();
        User userLogin = userService.loginByEmail(emailStr, varCode, varCodeText.getText());
        if (userLogin==null){
            return;
        }
        varCode="";    //清除验证码
        Client client = new Client(userLogin,null);
        client.setVisible(true);
        //关闭登录界面
        setVisible(false);
    }

    /**用户注册
     *
     * @param e
     */
    private void button1ActionPerformed(ActionEvent e) {
        UserRegistration userRegistration = new UserRegistration();
        userRegistration.setVisible(true);
    }

    /**
     * 忘记密码
     * @param e
     */
    private void forgotPasswordActionPerformed(ActionEvent e) {
        RetrievePassword retrievePassword = new RetrievePassword();
        retrievePassword.setVisible(true);
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        tabbedPane1 = new JTabbedPane();
        panel1 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        username = new JTextField();
        UserLoginByPassWord = new JButton();
        button2 = new JButton();
        password = new JPasswordField();
        forgotPassword = new JButton();
        panel2 = new JPanel();
        panel4 = new JPanel();
        label5 = new JLabel();
        emailText = new JTextField();
        label6 = new JLabel();
        varCodeText = new JTextField();
        sendVarCode = new JButton();
        LoginByEamliButton = new JButton();
        panel3 = new JPanel();
        label3 = new JLabel();
        textField1 = new JTextField();
        label4 = new JLabel();
        textField2 = new JTextField();
        button3 = new JButton();
        LoginByphone = new JButton();
        panel5 = new JPanel();
        label7 = new JLabel();
        label8 = new JLabel();
        username2 = new JTextField();
        AdminLoginButton = new JButton();
        button8 = new JButton();
        password2 = new JPasswordField();
        regist = new JButton();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                thisWindowClosed(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== tabbedPane1 ========
        {

            //======== panel1 ========
            {
                panel1.setLayout(null);

                //---- label1 ----
                label1.setText("\u7528\u6237\u540d:");
                panel1.add(label1);
                label1.setBounds(30, 55, 55, 40);

                //---- label2 ----
                label2.setText("\u5bc6\u7801:");
                panel1.add(label2);
                label2.setBounds(30, 115, 55, 40);

                //---- username ----
                username.setText("lisi");
                panel1.add(username);
                username.setBounds(84, 62, 230, username.getPreferredSize().height);

                //---- UserLoginByPassWord ----
                UserLoginByPassWord.setText("\u767b\u5f55");
                UserLoginByPassWord.addActionListener(e -> UserLoginByPassWordActionPerformed(e));
                panel1.add(UserLoginByPassWord);
                UserLoginByPassWord.setBounds(80, 170, UserLoginByPassWord.getPreferredSize().width, 29);

                //---- button2 ----
                button2.setText("\u53d6\u6d88");
                button2.addActionListener(e -> button2ActionPerformed(e));
                panel1.add(button2);
                button2.setBounds(237, 171, 78, 30);

                //---- password ----
                password.setText("111");
                panel1.add(password);
                password.setBounds(87, 122, 220, password.getPreferredSize().height);

                //---- forgotPassword ----
                forgotPassword.setText("\u5fd8\u8bb0\u5bc6\u7801");
                forgotPassword.addActionListener(e -> forgotPasswordActionPerformed(e));
                panel1.add(forgotPassword);
                forgotPassword.setBounds(new Rectangle(new Point(300, 10), forgotPassword.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel1.getComponentCount(); i++) {
                        Rectangle bounds = panel1.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel1.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel1.setMinimumSize(preferredSize);
                    panel1.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u5bc6\u7801\u767b\u5f55", panel1);

            //======== panel2 ========
            {
                panel2.setLayout(null);

                //======== panel4 ========
                {
                    panel4.setLayout(null);

                    //---- label5 ----
                    label5.setText("\u90ae\u7bb1:");
                    panel4.add(label5);
                    label5.setBounds(new Rectangle(new Point(40, 60), label5.getPreferredSize()));
                    panel4.add(emailText);
                    emailText.setBounds(95, 55, 215, emailText.getPreferredSize().height);

                    //---- label6 ----
                    label6.setText("\u9a8c\u8bc1\u7801:");
                    panel4.add(label6);
                    label6.setBounds(new Rectangle(new Point(40, 130), label6.getPreferredSize()));
                    panel4.add(varCodeText);
                    varCodeText.setBounds(100, 125, 100, varCodeText.getPreferredSize().height);

                    //---- sendVarCode ----
                    sendVarCode.setText("\u53d1\u9001\u9a8c\u8bc1\u7801");
                    sendVarCode.addActionListener(e -> sendVarCodeActionPerformed(e));
                    panel4.add(sendVarCode);
                    sendVarCode.setBounds(new Rectangle(new Point(230, 125), sendVarCode.getPreferredSize()));

                    //---- LoginByEamliButton ----
                    LoginByEamliButton.setText("\u767b\u5f55");
                    LoginByEamliButton.addActionListener(e -> LoginByEamliButtonActionPerformed(e));
                    panel4.add(LoginByEamliButton);
                    LoginByEamliButton.setBounds(new Rectangle(new Point(150, 185), LoginByEamliButton.getPreferredSize()));

                    { // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel4.getComponentCount(); i++) {
                            Rectangle bounds = panel4.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel4.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel4.setMinimumSize(preferredSize);
                        panel4.setPreferredSize(preferredSize);
                    }
                }
                panel2.add(panel4);
                panel4.setBounds(0, 0, 400, 238);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u90ae\u7bb1\u9a8c\u8bc1\u7801", panel2);

            //======== panel3 ========
            {
                panel3.setLayout(null);

                //---- label3 ----
                label3.setText("\u624b\u673a\u53f7\u7801:");
                panel3.add(label3);
                label3.setBounds(new Rectangle(new Point(35, 60), label3.getPreferredSize()));
                panel3.add(textField1);
                textField1.setBounds(100, 50, 215, textField1.getPreferredSize().height);

                //---- label4 ----
                label4.setText("\u9a8c\u8bc1\u7801:");
                panel3.add(label4);
                label4.setBounds(new Rectangle(new Point(40, 130), label4.getPreferredSize()));
                panel3.add(textField2);
                textField2.setBounds(105, 125, 100, textField2.getPreferredSize().height);

                //---- button3 ----
                button3.setText("\u53d1\u9001\u9a8c\u8bc1\u7801");
                panel3.add(button3);
                button3.setBounds(new Rectangle(new Point(230, 125), button3.getPreferredSize()));

                //---- LoginByphone ----
                LoginByphone.setText("\u767b\u5f55");
                panel3.add(LoginByphone);
                LoginByphone.setBounds(new Rectangle(new Point(150, 185), LoginByphone.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel3.getComponentCount(); i++) {
                        Rectangle bounds = panel3.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel3.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel3.setMinimumSize(preferredSize);
                    panel3.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u624b\u673a\u53f7\u7801\u767b\u5f55", panel3);

            //======== panel5 ========
            {
                panel5.setLayout(null);

                //---- label7 ----
                label7.setText("\u7ba1\u7406\u5458\u767b\u5f55\u540d:");
                panel5.add(label7);
                label7.setBounds(15, 40, 85, 40);

                //---- label8 ----
                label8.setText("\u5bc6\u7801:");
                panel5.add(label8);
                label8.setBounds(20, 110, 55, 40);

                //---- username2 ----
                username2.setText("admin");
                panel5.add(username2);
                username2.setBounds(100, 50, 230, 24);

                //---- AdminLoginButton ----
                AdminLoginButton.setText("\u767b\u5f55");
                AdminLoginButton.addActionListener(e -> AdminLoginButtonActionPerformed(e));
                panel5.add(AdminLoginButton);
                AdminLoginButton.setBounds(80, 180, 74, 24);

                //---- button8 ----
                button8.setText("\u53d6\u6d88");
                button8.addActionListener(e -> button2ActionPerformed(e));
                panel5.add(button8);
                button8.setBounds(240, 175, 78, 30);

                //---- password2 ----
                password2.setText("admin");
                panel5.add(password2);
                password2.setBounds(100, 120, 220, 25);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel5.getComponentCount(); i++) {
                        Rectangle bounds = panel5.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel5.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel5.setMinimumSize(preferredSize);
                    panel5.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u7ba1\u7406\u5458\u767b\u5f55", panel5);

            //---- regist ----
            regist.setText("\u7528\u6237\u6ce8\u518c");
            regist.setFont(regist.getFont().deriveFont(regist.getFont().getSize() + 25f));
            regist.addActionListener(e -> button1ActionPerformed(e));
            tabbedPane1.addTab("\u6ce8\u518c", regist);
        }
        contentPane.add(tabbedPane1);
        tabbedPane1.setBounds(5, 5, 400, 270);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTabbedPane tabbedPane1;
    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JTextField username;
    private JButton UserLoginByPassWord;
    private JButton button2;
    private JPasswordField password;
    private JButton forgotPassword;
    private JPanel panel2;
    private JPanel panel4;
    private JLabel label5;
    private JTextField emailText;
    private JLabel label6;
    private JTextField varCodeText;
    private JButton sendVarCode;
    private JButton LoginByEamliButton;
    private JPanel panel3;
    private JLabel label3;
    private JTextField textField1;
    private JLabel label4;
    private JTextField textField2;
    private JButton button3;
    private JButton LoginByphone;
    private JPanel panel5;
    private JLabel label7;
    private JLabel label8;
    private JTextField username2;
    private JButton AdminLoginButton;
    private JButton button8;
    private JPasswordField password2;
    private JButton regist;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
